import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/21 14:39
 * @description 递推
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int longestSubarray(int[] nums) {
        int n = nums.length;
        // prefix[i]为 以i结束的子数组中连续1的个数
        int[] prefix = new int[n];
        // suffix[i]为 以i开头的子树组中连续1的个数
        int[] suffix = new int[n];

        prefix[0] = nums[0];
        for (int i = 1; i < n; i++) {
            prefix[i] = nums[i] != 0 ? prefix[i - 1] + 1 : 0;
        }
        suffix[n - 1] = nums[n - 1];
        for(int i = n - 2; i >= 0; i--) {
            suffix[i] = nums[i] != 0 ? suffix[i + 1] + 1 : 0;
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int preSum = i == 0 ? 0 : prefix[i - 1];
            int sufSum = i == n - 1 ? 0 : suffix[i + 1];
            ans = Math.max(ans, preSum + sufSum);
        }
        return ans;
    }
}
